$(function() {
	var address_id = GetQueryString('address_id');
	var key = getcookie('key');

	$.ajax({
		type: 'post',
		url: ApiUrl + '/index.php?act=member_address&op=address_info',
		data: {
			key: key,
			address_id: address_id
		},
		dataType: 'json',
		success: function(result) {
			checklogin(result.login);
			var addrstr = result.datas.address_info.area_info;
			$('#detailaddr').html(addrstr);
			$(".detail-d-addr").html( result.datas.address_info.address);
			$('input[name=true_name]').val(result.datas.address_info.true_name);
			$('input[name=mob_phone]').val(result.datas.address_info.mob_phone);
			$('input[name=tel_phone]').val(result.datas.address_info.tel_phone);
			$('input[name=province_id]').val(result.datas.address_info.province_id);
			$('input[name=area_id]').val(result.datas.address_info.area_id);
			$('input[name=city_id]').val(result.datas.address_info.city_id);
			$('input[name=area_info]').val(result.datas.address_info.area_info);
			$('.address').val(result.datas.address_info.address);
			$('#address').val(result.datas.address_info.address);
			if (result.datas.address_info.is_default=='1') {
				$("#is_default1").attr("checked",'checked'); 
			} else{
				$("#is_default2").attr("checked",'checked'); 
			};
		}
	});

	$.ajax({
		type: 'post',
		url: ApiUrl + '/index.php?act=member_address&op=area_list',
		data: {
			key: key
		},
		dataType: 'json',
		success: function(result) {
			checklogin(result.login);
			var data = result.datas;
			var prov_html = '';
			for (var i = 0; i < data.area_list.length; i++) {
				prov_html += '<option value="' + data.area_list[i].area_id + '">' + data.area_list[i].area_name + '</option>';
			}
			$("select[name=prov]").append(prov_html);
		}
	});

	$("select[name=prov]").change(function() {
		var prov_id = $(this).val();
		$('input[name=province_id]').val(prov_id);
		$.ajax({
			type: 'post',
			url: ApiUrl + '/index.php?act=member_address&op=area_list',
			data: {
				key: key,
				area_id: prov_id
			},
			dataType: 'json',
			success: function(result) {
				checklogin(result.login);
				var data = result.datas;
				var city_html = '<option value="">请选择...</option>';
				for (var i = 0; i < data.area_list.length; i++) {
					city_html += '<option value="' + data.area_list[i].area_id + '">' + data.area_list[i].area_name + '</option>';
				}
				$("select[name=city]").html(city_html);
				$("select[name=region]").html('<option value="">请选择...</option>');
			}
		});
	});

	$("select[name=city]").change(function() {
		var city_id = $(this).val();
		$('input[name=city_id]').val(city_id);
		$.ajax({
			type: 'post',
			url: ApiUrl + '/index.php?act=member_address&op=area_list',
			data: {
				key: key,
				area_id: city_id
			},
			dataType: 'json',
			success: function(result) {
				checklogin(result.login);
				var data = result.datas;
				var region_html = '<option value="">请选择...</option>';
				for (var i = 0; i < data.area_list.length; i++) {
					region_html += '<option value="' + data.area_list[i].area_id + '">' + data.area_list[i].area_name + '</option>';
				}
				$("select[name=region]").html(region_html);
			}
		});
	});

	$("select[name=region]").change(function() {
		var prov_index = $('select[name=prov]')[0].selectedIndex;
		var city_index = $('select[name=city]')[0].selectedIndex;
		var region_index = $('select[name=region]')[0].selectedIndex;
		var area_info = $('select[name=prov]')[0].options[prov_index].innerHTML + ' ' + $('select[name=city]')[0].options[city_index].innerHTML + ' ' + $('select[name=region]')[0].options[region_index].innerHTML;
		$('input[name=area_info').val(area_info);
	});

	$('#editaddress').click(function() {
		if ($('input[name=modifyaddr]').val() == '1') {
			$('input[name=modifyaddr]').val(2);
			$('#area').show();
		} else {
			$('input[name=modifyaddr]').val(1);
			$('#area').hide();
		}
	});

	$.sValid.init({
		rules:{
			true_name:"required",
			mob_phone:"required",
			province_id:"required",
			city_id:"required",
			area_id:"required",
			address:"required"
		},
		messages:{
			true_name:"姓名必填！",
			mob_phone:"手机号必填！",
			province_id:"省份必填！",
			city_id:"城市必填！",
			area_id:"区县必填！",
			address:"街道必填！"
		},
		callback:function (eId,eMsg,eRules){
			if(eId.length >0){
				var errorHtml = "";
				$.map(eMsg,function (idx,item){
					errorHtml += "<p>"+idx+"</p>";
				});
				$(".error-tips").html(errorHtml).show();
			}else{
				 $(".error-tips").html("").hide();
			}
		}  
	});
	$('.add_address').click(function() {
		if($.sValid()){
			var true_name = $('input[name=true_name]').val();
			var mob_phone = $('input[name=mob_phone]').val();
			var tel_phone = $('input[name=tel_phone]').val();

			var op = $('input[name=modifyaddr]').val();
			if (op == '2') {
				var province_id = $('select[name=prov]').val();
				var city_id = $('select[name=city]').val();
				var area_id = $('select[name=region]').val();
				var address = $('input[name=address]').val();
				var prov_index = $('select[name=prov]')[0].selectedIndex;
				var city_index = $('select[name=city]')[0].selectedIndex;
				var region_index = $('select[name=region]')[0].selectedIndex;
				var area_info = $('select[name=prov]')[0].options[prov_index].innerHTML + ' ' + $('select[name=city]')[0].options[city_index].innerHTML + ' ' + $('select[name=region]')[0].options[region_index].innerHTML;
			} else {
				var province_id = $('input[name=province_id]').val();
				var city_id = $('input[name=city_id]').val();
				var area_id = $('input[name=area_id]').val();
				var address = $('input[name=address]').val();
				var area_info = $('input[name=area_info]').val();
			}
			var sdefault = $('.default');
			var is_default='0';
			for(var i =0;i<sdefault.length;i++){
				if(sdefault[i].checked){
					is_default = sdefault[i].value;
				}
			}
			var data = {
					key: key,
					true_name: true_name,
					mob_phone: mob_phone,
					tel_phone: tel_phone,
					province_id:province_id,
					city_id: city_id,
					area_id: area_id,
					address: address,
					area_info: area_info,
					address_id: address_id,
					is_default:is_default
				};
			$.ajax({
				type: 'post',
				url: ApiUrl + "/index.php?act=member_address&op=address_edit",
				data: data,
				dataType: 'json',
				success: function(result) {
					console.log(JSON.stringify(result));
					if (result) {
						location.href = WapSiteUrl + '/tmpl/member/address_list.html';
					} else {
						location.href = WapSiteUrl;
					}
				}
			});
		}
	});
});